package com.withouther.robot.rainingrobot.util.ai.dialogue;

        import cn.hutool.http.HttpStatus;
        import cn.hutool.setting.dialect.Props;
        import com.alibaba.fastjson.JSON;
        import com.alibaba.fastjson.JSONArray;
        import com.alibaba.fastjson.JSONObject;
        import com.aliyuncs.CommonRequest;
        import com.aliyuncs.CommonResponse;
        import com.aliyuncs.DefaultAcsClient;
        import com.aliyuncs.exceptions.ClientException;
        import com.aliyuncs.http.MethodType;
        import com.aliyuncs.profile.DefaultProfile;
        import com.aliyuncs.profile.IClientProfile;
        import com.withouther.robot.rainingrobot.util.ai.dialogue.model.BotResult;
        import lombok.extern.slf4j.Slf4j;

        import java.util.HashMap;
        import java.util.Map;

/**
 * @ClassName BeeBot
 * @Auther: tzq
 * @Date: 2020/9/21 14:00
 * @Description:
 */
@Slf4j
public class BeeBot implements DialogueBot {

    private static DefaultAcsClient client = null;

    public BeeBot() {
        init();
    }
    public void init(){
        Props robotPro = new Props("robot.properties");
        String accountAccessAK = robotPro.getStr("ali.accountAccessAK");
        String accountAccessSK = robotPro.getStr("ali.accountAccessSK");
        String popRegion = robotPro.getStr("ali.popRegion");
        String popProduct = robotPro.getStr("ali.popProduct");
        String popDomain = robotPro.getStr("ali.popDomain");
        if (client == null) {
            DefaultProfile.addEndpoint(popRegion, popProduct, popDomain);
            IClientProfile profile = DefaultProfile.getProfile(popRegion, accountAccessAK, accountAccessSK);
            client = new DefaultAcsClient(profile);
        }
    }
    @Override
    public BotResult getDialogue(Map<String, Object> paramMap) throws ClientException {
        //固定入参
        CommonRequest commonRequest = new CommonRequest();
        commonRequest.setSysProduct("Chatbot");
        commonRequest.setSysMethod(MethodType.GET);
        //根据API会有变化
        commonRequest.setSysAction("Chat");
        commonRequest.setSysVersion("2017-10-11");
        commonRequest.putQueryParameter("Utterance", (String) paramMap.get("text"));
        String InstanceId = paramMap.get("InstanceId") == null ? "chatbot-cn-q6YfyVfq6e" : (String) paramMap.get("InstanceId");
        commonRequest.putQueryParameter("InstanceId", InstanceId);
        CommonResponse commonResponse = post(commonRequest);
        BotResult botResult = new BotResult();
        botResult.setSuccess(commonResponse.getHttpStatus() == HttpStatus.HTTP_OK);
        JSONObject data = JSON.parseObject(commonResponse.getData());
        JSONArray messages = data.getJSONArray("Messages");
        JSONObject textJson = messages.getJSONObject(0).getJSONObject("Text");
        botResult.setText(textJson.getString("Content"));
        botResult.setRequestId(data.getString("RequestId"));
        botResult.setMessage(textJson.getString("AnswerSource"));
        return botResult;
    }
    public CommonResponse post(CommonRequest commonRequest) throws ClientException {
        CommonResponse commonResponse = client.getCommonResponse(commonRequest);
        log.info("dialogueRequest: {}", JSON.toJSONString(commonRequest));
        log.info("dialogueResponse: {}", JSON.toJSONString(commonResponse));
        return commonResponse;
    }
}
